package com.bjsxt;

import com.bjsxt.service.BusinessService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableFeignClients
public class BusinessServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(BusinessServiceApplication.class ,args) ;
    }

    @Autowired
    private BusinessService businessService ;


    @GlobalTransactional // 在分布式事务里面它会有一个全局的事务的ID
    @GetMapping("/purchase/{userId}/{productNo}/{count}")
    public ResponseEntity<String> purchase(
            @PathVariable("userId") String userId ,
            @PathVariable("productNo") String productNo ,
            @PathVariable("count") Integer count
    ){
        String xid = RootContext.getXID();
        System.out.println("全局的事务id:"+xid);
        businessService.purchase(userId ,productNo ,count);
        return ResponseEntity.ok("下单成功") ;
    }
}
